<template>
  <div class="md5-tool">
    <button @click="returnFn" class="btn btn-primary retrunBUtton">
      <i class="icon">⚡</i> 返回
    </button>
    <div class="container">
      <header class="tool-header">
        <h1>MD5 加密工具</h1>
        <p class="subtitle">使用Vue 3和CryptoJS实现的MD5哈希生成器</p>
      </header>

      <div class="tool-content">
        <div class="input-section">
          <label for="inputText">输入要加密的文本：</label>
          <textarea
            id="inputText"
            v-model="inputText"
            placeholder="在此输入要计算MD5哈希值的文本..."
            autofocus
            rows="5"
          ></textarea>
        </div>

        <div class="result-section">
          <div class="result-header">
            <label>MD5 哈希值：</label>
            <button
              class="copy-btn"
              @click="copyToClipboard"
              :disabled="!md5Hash"
              :class="{ copied: copyButtonText === '已复制!' }"
            >
              {{ copyButtonText }}
            </button>
          </div>
          <div class="hash-result">
            {{ md5Hash || '输入文本后，将在此显示MD5哈希值' }}
          </div>
        </div>

        <div class="info-section">
          <h3>关于MD5加密</h3>
          <p>
            MD5（Message Digest Algorithm
            5）是一种广泛使用的密码散列函数，可以产生一个128位（16字节）的散列值。虽然MD5曾经被广泛用于密码存储和数据完整性验证，但由于其存在安全漏洞，现在不推荐用于安全敏感的应用。
          </p>
        </div>
      </div>

      <footer class="tool-footer">
        <p>© 2023 Vue 3 MD5加密工具 | 使用CryptoJS库</p>
      </footer>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import CryptoJS from 'crypto-js'
import { useRouter } from 'vue-router'
const inputText = ref('')
const copyButtonText = ref('复制到剪贴板')
const Router = useRouter()
const returnFn = () => {
  Router.go(-1)
}

// 计算MD5哈希值
const md5Hash = computed(() => {
  if (!inputText.value.trim()) return ''
  return CryptoJS.MD5(inputText.value).toString()
})

// 复制到剪贴板功能
const copyToClipboard = async () => {
  if (!md5Hash.value) return

  try {
    await navigator.clipboard.writeText(md5Hash.value)
    copyButtonText.value = '已复制!'
    setTimeout(() => {
      copyButtonText.value = '复制到剪贴板'
    }, 2000)
  } catch (err) {
    console.error('复制失败: ', err)
    // 降级方案：使用document.execCommand
    const textArea = document.createElement('textarea')
    textArea.value = md5Hash.value
    document.body.appendChild(textArea)
    textArea.select()
    try {
      document.execCommand('copy')
      copyButtonText.value = '已复制!'
      setTimeout(() => {
        copyButtonText.value = '复制到剪贴板'
      }, 2000)
    } catch (err2) {
      copyButtonText.value = '复制失败'
      setTimeout(() => {
        copyButtonText.value = '复制到剪贴板'
      }, 2000)
    }
    document.body.removeChild(textArea)
  }
}
</script>

<style scoped>
.btn {
  padding: 8px 15px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 20px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: all 0.3s ease;
}

.btn-primary {
  background: linear-gradient(to right, #1a2a6c, #b21f1f);
  color: white;
}
.retrunBUtton {
  position: absolute;
  top: 100px;
  right: 100px;
}

.md5-tool {
  min-height: 100vh;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  position: relative;
}

.container {
  width: 100%;
  max-width: 1200px;
  background-color: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.tool-header {
  background: linear-gradient(to right, #4a00e0, #8e2de2);
  color: white;
  padding: 25px 30px;
  text-align: center;
}

.tool-header h1 {
  font-size: 28px;
  margin-bottom: 10px;
  font-weight: 600;
}

.subtitle {
  font-size: 16px;
  opacity: 0.9;
}

.tool-content {
  padding: 30px;
}

.input-section {
  margin-bottom: 30px;
}

.input-section label {
  display: block;
  margin-bottom: 10px;
  font-weight: 600;
  color: #333;
  font-size: 16px;
}

textarea {
  width: 100%;
  padding: 15px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 16px;
  resize: vertical;
  transition: border-color 0.3s;
  font-family: inherit;
}

textarea:focus {
  outline: none;
  border-color: #6a11cb;
}

.result-section {
  background-color: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  margin-top: 20px;
}

.result-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.result-header label {
  font-weight: 600;
  color: #333;
  font-size: 16px;
}

.copy-btn {
  background: linear-gradient(to right, #4a00e0, #8e2de2);
  color: white;
  border: none;
  padding: 8px 15px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.copy-btn:hover:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.copy-btn:active:not(:disabled) {
  transform: translateY(0);
}

.copy-btn:disabled {
  background: #cccccc;
  cursor: not-allowed;
}

.copy-btn.copied {
  background: #4caf50;
}

.hash-result {
  font-family: 'Courier New', monospace;
  background-color: #2d3748;
  color: #68d391;
  padding: 15px;
  border-radius: 5px;
  word-break: break-all;
  font-size: 16px;
  line-height: 1.5;
  min-height: 60px;
  display: flex;
  align-items: center;
}

.info-section {
  margin-top: 30px;
  background-color: #e3f2fd;
  border-left: 4px solid #2196f3;
  padding: 15px;
  border-radius: 0 8px 8px 0;
}

.info-section h3 {
  color: #1565c0;
  margin-bottom: 10px;
  font-size: 18px;
}

.info-section p {
  color: #424242;
  line-height: 1.6;
  font-size: 14px;
}

.tool-footer {
  text-align: center;
  padding: 20px;
  color: #757575;
  font-size: 14px;
  border-top: 1px solid #e0e0e0;
}

@media (max-width: 600px) {
  .md5-tool {
    padding: 10px;
  }

  .container {
    border-radius: 10px;
  }

  .tool-header h1 {
    font-size: 24px;
  }

  .tool-content {
    padding: 20px;
  }

  .result-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .copy-btn {
    align-self: flex-end;
  }
}
</style>